package com.easysoft.framework.chat.service;

import com.easysoft.commons.utils.UUIDUtils;
import com.easysoft.framework.annotation.jdbc.DataSource;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.chat.mapper.SysChatsMapperExtd;
import com.easysoft.framework.chat.model.SysChats;
import com.easysoft.framework.chat.model.SysChatsQuery;
import com.easysoft.framework.chat.model.SysChatsView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by try on 2017/8/17.
 */
@Service("service.SysChatsService")
public class SysChatsService {
    @Autowired
    @Qualifier("mapper.SysChatsMapperExtd")
    private SysChatsMapperExtd sysChatsMapperExtd;

    @DataSource(DataSource.MASTER)
    public int addSysChats(SysChats entiry){
        entiry.setId(UUIDUtils.getUUID());
        entiry.setCreateTime(new Date());
        return sysChatsMapperExtd.insertSelective(entiry);
    }

    @DataSource(DataSource.MASTER)
    public int deleteSysChats(String id){
        return sysChatsMapperExtd.deleteByPrimaryKey(id);
    }

    @DataSource(DataSource.MASTER)
    public int updateSysChats(SysChats entiry){
        return sysChatsMapperExtd.updateByPrimaryKeySelective(entiry);
    }

    @DataSource(DataSource.MASTER)
    public SysChats selectSysChatsById(String id){
        return sysChatsMapperExtd.selectByPrimaryKey(id);
    }


    @DataSource(DataSource.SLAVE)
    public List<SysChatsView> selectSysChatsListByPage(SysChatsQuery query, Page page){
        int maxRow = sysChatsMapperExtd.selectCountSysChats(query);
        page.setMaxRow(maxRow);

        if (maxRow == 0) {
            return new ArrayList<SysChatsView>();
        }
        return sysChatsMapperExtd.selectSysChatsListByPage(query,page);
    }
}
